package com.rajaev.dtu.tractor.infrastructure.helper;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringBeanUtils implements ApplicationContextAware {

    private volatile static ApplicationContext applicationContext;

    public static <T> T getBean(String name, Class<T> clazz) {
        return applicationContext.getBean(name, clazz);
    }


    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

    @Override
    public void setApplicationContext(ApplicationContext app) throws BeansException {
        if (SpringBeanUtils.applicationContext == null) {
            synchronized (SpringBeanUtils.class) {
                if (SpringBeanUtils.applicationContext == null) {
                    SpringBeanUtils.applicationContext = app;
                }
            }
        }
    }


}
